// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;


namespace DataModel
{
    public class GedComFile : GedComNode
    {
        private GedComFile()
        {
        }


        public static GedComFile Read(FileInfo info, GedComReadResults results)
        {
            GedComFile file;

            // Set up the xref entry list...this holds the nodes that have xref entries so
            // we can find them later...
            Dictionary<string, GedComNode> xrefEntries = new Dictionary<string, GedComNode>();

            // Read the raw data...
            using (GedComReader reader = new GedComReader(info))
            {
                file = new GedComFile();
                file.Parse(reader, xrefEntries);
            }

            // Now that we have a (theoretically) complete set of xrefs, go through and link
            // them together...
            file.SetXrefs(xrefEntries, results);

            // Return what we came up with...
            return file;
        }


        public void WriteDebugXml(FileInfo info)
        {
            // Start with a clean slate
            if (info.Exists)
            {
                info.Delete();
            }

            // Dump everything out...
            using (FileStream outputStream = info.OpenWrite())
            {
                XmlWriterSettings xmlSettings = new XmlWriterSettings();
                xmlSettings.Indent = true;
                xmlSettings.IndentChars = "    ";

                using (XmlWriter writer = XmlWriter.Create(outputStream, xmlSettings))
                {
                    this.WriteDebugXmlMinion(writer);
                    writer.Close();
                }

                outputStream.Close();
            }
        }

    }
}
